package edu.hawaii.halealohacli.command;

import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.sensordata.jaxb.SensorData;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Returns the current power consumption of a given source.
 * 
 * @author David Wilkie
 * @author Branden Ogata
 */
public class CurrentPower implements Command {

  /** Used for testing. */
  private Double powerConsumed;

  /**
   * Gets power consumed.
   * 
   * @return the powerConsumed
   */
  public Double getPowerConsumed() {
    return this.powerConsumed;
  }

  /**
   * Prints the current power of source on days[0].
   * @param client The WattDepotClient to query.
   * @param source the source to get current power from.
   * @param days ignored
   * @throws Exception upon error with the server.
   */
  @Override public void printResults(WattDepotClient client, Source source,
      XMLGregorianCalendar... days) throws Exception {
    String sourceName = source.getName();
    SensorData data = client.getLatestSensorData(sourceName);
    Double powerConsumed = Double.parseDouble(data.getProperty("powerConsumed")); 
    this.powerConsumed = powerConsumed;
    String time = data.getTimestamp().toString();
    System.out.format("%s's power as of %s was %.2f kW.\n", sourceName, time, powerConsumed / 1000);
  }
}
